using JGSY.CMS.LowCode.Platform.Domain.Entities;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;
using JGSY.CMS.LowCode.Platform.Infrastructure.CmsDbContext;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Repositories
{
    /// <summary>
    /// 双因子认证仓储实现
    /// 提供双因子认证配置管理的数据访问功能实现，使用Entity Framework进行数据持久化
    /// </summary>
    public class TwoFactorRepository : ITwoFactorRepository
    {
        /// <summary>
        /// 数据库上下文
        /// </summary>
        private readonly LowCodeDbContext _db;

        /// <summary>
        /// 初始化双因子认证仓储实例
        /// </summary>
        /// <param name="db">数据库上下文</param>
        public TwoFactorRepository(LowCodeDbContext db)
        {
            _db = db;
        }

        /// <summary>
        /// 获取所有双因子认证记录
        /// </summary>
        /// <returns>双因子认证记录集合</returns>
        public async Task<IEnumerable<TwoFactor>> GetAllAsync()
        {
            return await _db.TwoFactors.ToListAsync();
        }

        /// <summary>
        /// 根据标识符获取双因子认证记录
        /// </summary>
        /// <param name="id">双因子认证标识符</param>
        /// <returns>双因子认证记录，如果不存在则返回null</returns>
        public async Task<TwoFactor?> GetByIdAsync(long id)
        {
            return await _db.TwoFactors.FindAsync(id);
        }

        /// <summary>
        /// 根据用户标识符获取双因子认证记录
        /// </summary>
        /// <param name="userId">用户标识符</param>
        /// <returns>该用户的双因子认证记录，如果不存在则返回null</returns>
        public async Task<TwoFactor?> GetByUserIdAsync(long userId)
        {
            return await _db.TwoFactors.FirstOrDefaultAsync(x => x.UserId == userId);
        }

        /// <summary>
        /// 添加新的双因子认证记录
        /// </summary>
        /// <param name="entity">要添加的双因子认证实体</param>
        /// <returns>添加操作的异步任务</returns>
        public async Task AddAsync(TwoFactor entity)
        {
            _db.TwoFactors.Add(entity);
            await _db.SaveChangesAsync();
        }

        /// <summary>
        /// 更新双因子认证记录
        /// </summary>
        /// <param name="id">双因子认证标识符</param>
        /// <param name="entity">更新后的双因子认证实体</param>
        /// <returns>更新操作的异步任务</returns>
        public async Task UpdateAsync(long id, TwoFactor entity)
        {
            var existing = await _db.TwoFactors.FindAsync(id);
            if (existing != null)
            {
                _db.Entry(existing).CurrentValues.SetValues(entity);
                await _db.SaveChangesAsync();
            }
        }

        /// <summary>
        /// 删除双因子认证记录
        /// </summary>
        /// <param name="id">双因子认证标识符</param>
        /// <returns>删除操作的异步任务</returns>
        public async Task DeleteAsync(long id)
        {
            var entity = await _db.TwoFactors.FindAsync(id);
            if (entity != null)
            {
                _db.TwoFactors.Remove(entity);
                await _db.SaveChangesAsync();
            }
        }
    }
}
